<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\StaffService;
use App\Utility\Upload;
use Illuminate\Http\Request;

class StaffController extends BaseController
{
    //登录

    //获取权限
    public function index(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $pas['role_id'] = $request->post('role_id', 0);
        $res = StaffService::index($pages, $pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data'], $res['total']);
    }

    //选项
    public function getOption(Request $request)
    {
        $res = StaffService::getOption();
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }

    //修改
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['title'] = $request->post('title', '');
        $pas['role_id'] = $request->post('role_id', 0);
        $pas['shop_id'] = $request->post('shop_id', 0);
        $head = $request->post('head', '');
        if ($head) {
            $pas['head'] = Upload::getPathByBase64($head);
        }
        if ($id) {
            $res = StaffService::modify($id, $pas);
        } else {
            $res = StaffService::add($pas);
        }
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson();
    }

    //启用禁用
    public function toable(Request $request)
    {
        $id = $request->post('id', 0);
        $able = $request->post('able', 0);
        $res = StaffService::toable($id, $able);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson();
    }

//    //删除
//    public function del(Request $request)
//    {
//        $id = $request->post('id', 0);
//        $res = StaffService::del($id);
//        if ($res['code'] < 0) {
//            return self::toJson(-2, $res['msg']);
//        }
//        return self::toJson();
//    }

    //重置密码
    public function reset(Request $request)
    {
        $id = $request->post('id', 0);
        $res = StaffService::reset($id);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson();
    }

    //修改密码
    public function pwdModify(Request $request)
    {
        $id = $request->post('id', 0);
        $pwd = $request->post('pwd', '');
        $pwd2 = $request->post('pwd2', '');
        $res = StaffService::pwdModify($id, $pwd, $pwd2);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson();
    }
}